package com.blt.car.home;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseFragment;
import com.blt.car.base.ViewHolder;
import com.blt.car.home.adapter.CarInfoAdapter;
import com.blt.car.utils.ActivityHelper;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import blt.a3rd.map.MapSdk;
import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.car.CarApi;
import blt.app_core.api.car.bean.Car;
import blt.app_core.api.message.MessageApi;
import blt.app_core.api.message.bean.BannerInfo;
import blt.app_core.api.message.bean.WeatherInfo;
import blt.app_core.api.other.OutApi;
import blt.app_core.log.Logger;
import blt.app_core.utils.UriProdiver;

/**
 * Created by xgp on 2017/6/13.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener {
    String url1 = "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size" +
            "=b4000_4000&sec=1497532170&di=d371150f7ab0cb11adb0b2d9c9f2792b&src=http://img.mp.sohu.com/upload/20170528/5c7688edbf944035b5848f6e69dc77f7_th.png";

    String url2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497542252147&di=28826a0af2733a0b591acc8550d7afc3&imgtype=0&src=http%3A%2F%2Fimg7.sjfzxm." +
            "com%2Fupload%2Fimage_remote%2F2017%2F05%2F20%2F6a082ce9-a14e-40bc-8e91-9b8a5a4824d7.jpg";

    String url3 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497542252146&di=2f2a672bd6b68cafedac0a4bdccff2d3&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2F" +
            "item%2F38dbb6fd5266d0162a371b379d2bd40735fa3586.jpg";

    private List<String> urls = new ArrayList<>();


    private GridView mGridView;
    private CarInfoAdapter infoAdapter;
    private ImageView mWheather;
    private TextView mAqi;

    private TextView cityName;
    private TextView temp;
    private TextView status;
    private Banner banner;
    private String city;


    List<BannerInfo> infos;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(ViewHolder holder) {
        banner = holder.get(R.id.home_banner);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setDelayTime(5000);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                ActivityHelper.toBannerDetail(getActivity(), infos.get(position).id);
            }
        });

        ImageView fastImage = holder.get(R.id.home_iv_map);
        temp = holder.get(R.id.home_weather_temperature);
        status = holder.get(R.id.home_weather_status);
        holder.get(R.id.home_app_message);
        mWheather = holder.get(R.id.home_weather_icon);
        cityName = holder.get(R.id.home_cityname);
        holder.get(R.id.home_change_car);
        mAqi = holder.get(R.id.home_weather_aqi);
        holder.setOnClickListener(this, R.id.home_cityname, R.id.home_app_message,
                R.id.home_change_car, R.id.home_iv_map);
//        ImageView imageView = holder.get(R.id.logo);
//        Glide.with(this).load(url1).bitmapTransform(new GlideCircleTransform(getActivity())).into(imageView);


        mGridView = holder.get(R.id.home_gridview);
        infoAdapter = new CarInfoAdapter(getContext());
        mGridView.setAdapter(infoAdapter);


    }

    @Override
    protected void initData() {
        super.initData();
//        urls.add(url1);
//        urls.add(url2);
//        urls.add(url3);
        MapSdk.getMapSdk().start();
        CoreManager.getCore(OutApi.class).getBanner();
        CoreManager.getCore(CarApi.class).getCarDetailInfo(0);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_cityname:
                ActivityHelper.toChoseCityActivity(getActivity(), city);
                break;
            case R.id.home_app_message:
                ActivityHelper.toMessageActivity(getActivity());
                break;
            case R.id.home_change_car:
                ActivityHelper.toCarListActivity(getActivity());
                break;
            case R.id.home_iv_map:
                ActivityHelper.toMapActivity(getActivity(), city);
                break;
        }
    }

    @CoreEvent(coreClientClass = MessageApi.class)
    public void onWeather(WeatherInfo.Weather weather) {
        int code = weather.now.cond.code;
        if (code == 101 || code == 100) {
            mWheather.setBackgroundResource(R.mipmap.weather_sun);
        } else if (code == 104) {
            mWheather.setBackgroundResource(R.mipmap.wheather_cloudy);
        } else if (code >= 300) {
            mWheather.setBackgroundResource(R.mipmap.weather_rainy);
        }
        temp.setText(weather.now.tmp + "°C");
        status.setText(weather.now.cond.txt);
        mAqi.setText("空气" + weather.aqi.city.aqi + " " + weather.aqi.city.qlty);
    }


    @CoreEvent(coreClientClass = OutApi.class)
    public void onBanner(List<BannerInfo> infos) {
        this.infos = infos;
        for (BannerInfo info : infos) {
            urls.add(UriProdiver.IMAGE_BASE_URL + info.picName);
        }
        banner.setImages(urls);//图片集合
        banner.start();
    }

    @CoreEvent(coreClientClass = OutApi.class)
    public void onLocation(String address) {
        city = address;
        cityName.setText(address);
        MapSdk.getMapSdk().stop();
        CoreManager.getCore(MessageApi.class).getWeather(address);
    }


    private List<String> carInfo = new ArrayList<>();

    @CoreEvent(coreClientClass = CarApi.class)
    public void onUpdataHomeCar(Car car) {
        if (car == null) {
            return;
        }
        carInfo.clear();
        carInfo.add(car.carPlateNumber);
        carInfo.add(car.carModel);
        if (car.carType == 0) {
            carInfo.add("小型客车");
        } else {
            carInfo.add("大型客车");
        }
        carInfo.add(car.carSeat + "座");
        for (int i = 0; i < 4; i++) {
            updataView(i);
        }
    }

    private void updataView(int index) {
        int visiblePosition = mGridView.getFirstVisiblePosition();
        View view = mGridView.getChildAt(index - visiblePosition);
        TextView data = (TextView) view.findViewById(R.id.item_homegird_data);
        data.setText(carInfo.get(index));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MapSdk.getMapSdk().clearSdk();
    }
}
